package com.example.lishop.service.impl

import com.example.lishop.entity.BProductEntity
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.repository.ProductRepository
import com.example.lishop.service.ProductService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.util.Assert

/**
 * Created on 2021/3/14 18:14
 * @author shuguo
 */
@Service
class ProductServiceImpl : ProductService {

    @Autowired
    lateinit var repository: ProductRepository

    override fun add(data: BProductEntity): BProductEntity {
        return repository.save(data)
    }

    override fun update(data: BProductEntity) {
        detail(data)
        return repository.update(data.productId, data.name, data.descript, data.type, data.tag, data.status, data.sts)
    }

    override fun del(data: BProductEntity) {
        detail(data)
        repository.del(data.productId)
    }

    override fun list(page:Int, size:Int): Page<BProductEntity> {
        val req: Pageable = PageRequest.of(page, size)
        return repository.findAll(req)
    }

    override fun detail(data: BProductEntity): BProductEntity? {
        val result = repository.findByProductId(data.productId)
        Assert.notNull(result, ExceptEnum.NOT_EXIST)
        return result
    }

    override fun listByBusinessId(businessId:String): List<BProductEntity> {
        return repository.findAllByBusinessId(businessId)
    }

}